package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class DtsAdminGoodsController {
    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsCategoryService categoryService;


    @RequestMapping("/create")
    public Object add(@RequestBody GoodsAllinone goodsAllinone) {
        Object result = goodsService.create(goodsAllinone);
        return result;
    }


    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods) {
        Object result = goodsService.delete(goods);
        return result;
    }


    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> catVos = categoryService.findList();
        List<Map<String,Object>> brands = brandService.findBrand();
        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",catVos) ;
        data.put("brandList",brands) ;
        return ResponseUtil.ok(data);
    }


    @RequestMapping("/detail")
    public Object detail(Integer id){
        if(id == null) {
            return AdminResponseUtil.badArgument();
        }
        Map<String,Object> data = goodsService.queryById(id);
        return ResponseUtil.ok(data);
    }
    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit", required = false,defaultValue = "10") Integer limit,
            String sort, String order ,String goodsSn ,String name){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admim = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = new ArrayList<Integer>();
        if(subject.hasRole("品牌制造商")) {
            brandIds = brandService.queryById(admim.getId());
        }
        IPage<DtsGoods> ipage = goodsService.queryPage(page,limit, sort, order, goodsSn, name, brandIds);
        Map<String,Object> data = new HashMap<>();
        data.put("total", ipage.getTotal() );
        data.put("items", ipage.getRecords() );
        return ResponseUtil.ok(data);
    }


    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){

        return goodsService.update(goodsAllinone);
    }
}
